home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / ASTErrorForm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  4.7 KB  |  136 lines  |  [TEXT/KAHL]

  1. /* ASTErrorForm.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "ASTErrorForm.h"
  31. #include "TrashTracker.h"
  32. #include "Memory.h"
  33. #include "ASTExpression.h"
  34.  
  35.  
  36. struct ASTErrorFormRec
  37.     {
  38.         ASTExpressionRec*        ResumeCondition;
  39.         char*                                MessageString;
  40.         long                                LineNumber;
  41.     };
  42.  
  43.  
  44. /* create a new AST error form */
  45. ASTErrorFormRec*        NewErrorForm(struct ASTExpressionRec* Expression, char* String,
  46.                                             struct TrashTrackRec* TrashTracker, long LineNumber)
  47.     {
  48.         ASTErrorFormRec*    ErrorForm;
  49.  
  50.         CheckPtrExistence(Expression);
  51.         ErrorForm = (ASTErrorFormRec*)AllocTrackedBlock(sizeof(ASTErrorFormRec),TrashTracker);
  52.         if (ErrorForm == NIL)
  53.             {
  54.                 return NIL;
  55.             }
  56.         SetTag(ErrorForm,"ASTErrorFormRec");
  57.  
  58.         ErrorForm->ResumeCondition = Expression;
  59.         ErrorForm->MessageString = String;
  60.         ErrorForm->LineNumber = LineNumber;
  61.  
  62.         return ErrorForm;
  63.     }
  64.  
  65.  
  66. /* type check the error message node.  this returns eCompileNoError if */
  67. /* everything is ok, and the appropriate type in *ResultingDataType. */
  68. CompileErrors                TypeCheckErrorForm(DataTypes* ResultingDataType,
  69.                                             ASTErrorFormRec* ErrorMessage, long* ErrorLineNumber,
  70.                                             struct TrashTrackRec* TrashTracker)
  71.     {
  72.         CompileErrors            Error;
  73.         DataTypes                    ResumeConditionType;
  74.  
  75.         CheckPtrExistence(ErrorMessage);
  76.         CheckPtrExistence(TrashTracker);
  77.  
  78.         Error = TypeCheckExpression(&ResumeConditionType,ErrorMessage->ResumeCondition,
  79.             ErrorLineNumber,TrashTracker);
  80.         if (Error != eCompileNoError)
  81.             {
  82.                 return Error;
  83.             }
  84.  
  85.         if (ResumeConditionType != eBoolean)
  86.             {
  87.                 *ErrorLineNumber = ErrorMessage->LineNumber;
  88.                 return eCompileErrorNeedsBooleanArg;
  89.             }
  90.  
  91.         *ResultingDataType = eBoolean;
  92.         return eCompileNoError;
  93.     }
  94.  
  95.  
  96. /* generate code for an error thing.  returns True if successful, or False if it fails. */
  97. MyBoolean                        CodeGenErrorForm(struct PcodeRec* FuncCode,
  98.                                             long* StackDepthParam, ASTErrorFormRec* ErrorForm)
  99.     {
  100.         long                            StackDepth;
  101.  
  102.         CheckPtrExistence(FuncCode);
  103.         CheckPtrExistence(ErrorForm);
  104.         StackDepth = *StackDepthParam;
  105.  
  106.         /* evaluate the resume condition */
  107.         if (!CodeGenExpression(FuncCode,&StackDepth,ErrorForm->ResumeCondition))
  108.             {
  109.                 return False;
  110.             }
  111.         ERROR(StackDepth != *StackDepthParam + 1,PRERR(ForceAbort,
  112.             "CodeGenErrorForm:  stack depth error evaluating resume condition"));
  113.  
  114.         /* do the thing */
  115.         if (!AddPcodeInstruction(FuncCode,epErrorTrap,NIL))
  116.             {
  117.                 return False;
  118.             }
  119.         if (!AddPcodeOperandString(FuncCode,ErrorForm->MessageString,
  120.             PtrSize(ErrorForm->MessageString)))
  121.             {
  122.                 return False;
  123.             }
  124.         StackDepth -= 1; /* consume operand */
  125.         ERROR(StackDepth != *StackDepthParam,PRERR(ForceAbort,
  126.             "CodeGenErrorForm:  stack depth error after trap"));
  127.  
  128.         /* error instruction leaves a return code, after consuming its operand. */
  129.         StackDepth += 1;
  130.         ERROR(StackDepth != *StackDepthParam + 1,PRERR(ForceAbort,
  131.             "CodeGenErrorForm:  stack depth error after pushing return value"));
  132.  
  133.         *StackDepthParam = StackDepth;
  134.         return True;
  135.     }
  136.